/*
 * Copyright (C) 2008
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.sca4idea.psi;

import com.googlecode.sca4idea.util.ComponentResolver;
import com.googlecode.sca4idea.util.SCDLResolver;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * PSI Element that provides introspection functionality for components defined by external composites.
 */
public class SCACompositeComponentTagPsiElement extends SCAComponentTagPsiElement {
    private XmlFile file;

    public SCACompositeComponentTagPsiElement(XmlTag underlying, String name, boolean isResource) {
        super(underlying);

        if (StringUtil.isEmpty(name)) {
            file = null;
        } else if (isResource){
            file = SCDLResolver.loadScdlResource(underlying, name);
        } else {
            file = SCDLResolver.loadScdlLocation(underlying, name);
        }
    }

    public PsiElement resolveServiceInterface(String serviceInterfaceName) {
        if (file == null) {
            return null;
        }

        // Retrieve all the promotions for the referenced file
        Map<String, XmlTag> promotions = ComponentResolver.getPromotedServices(file);
        return promotions.get(serviceInterfaceName);
    }

    public Object[] getVariantsOfServiceInterfaces() {
        if (file == null) {
            return new Object[0];
        }

        // Retrieve all the promotions for the referenced file
        Map<String, XmlTag> promotions = ComponentResolver.getPromotedServices(file);
        String componentName = getComponentName();
        List<String> result = new ArrayList<String>();
        for (String promotion : promotions.keySet()) {
            result.add(componentName + "/" + promotion);
        }

        return result.toArray();
    }

    public PsiElement resolveReference(String referenceName) {
        if (file == null) {
            return null;
        }

        // Retrieve all the references for the referenced file
        Map<String, XmlTag> references = ComponentResolver.getPromotedReferences(file, true);
        return references.get(referenceName);
    }

    public PromotedWireable[] getVariantsOfReferences(boolean qualified) {
        if (file == null) {
            return new PromotedWireable[0];
        }

        // Retrieve all the references for the referenced file
        Map<String, XmlTag> references = ComponentResolver.getPromotedReferences(file, true);
        String componentName = getComponentName();
        List<PromotedWireable> result = new ArrayList<PromotedWireable>();
        for (String reference : references.keySet()) {
            boolean optional = isOptional(references.get(reference));

            if (qualified) {
                result.add(new PromotedWireableImpl(componentName + "/" + reference, optional));
            } else {
                result.add(new PromotedWireableImpl(reference, optional));
            }
        }

        return result.toArray(new PromotedWireable[result.size()]);
    }

    public PromotedWireable[] getVariantsOfProperties(boolean qualified) {
        if (file == null) {
            return new PromotedWireable[0];
        }

        // Retrieve all the references for the referenced file
        Map<String, XmlTag> properties = ComponentResolver.getPromotedProperties(file, false);
        String componentName = getComponentName();
        List<PromotedWireable> result = new ArrayList<PromotedWireable>();
        for (String property : properties.keySet()) {
            boolean optional = isOptional(properties.get(property));

            if (qualified) {
                result.add(new PromotedWireableImpl(componentName + "/" + property, optional));
            } else {
                result.add(new PromotedWireableImpl(property, optional));
            }
        }

        return result.toArray(new PromotedWireable[result.size()]);
    }

    public PsiElement resolveProperty(String propertyName) {
        if (file == null) {
            return null;
        }

        // Retrieve all the references for the referenced file
        Map<String, XmlTag> references = ComponentResolver.getPromotedProperties(file, false);
        return references.get(propertyName);
    }

    public PsiElement getTarget() {
        return file;
    }

    private boolean isOptional(XmlTag tag) {
        String multiplicity = tag.getAttributeValue("multiplicity");
        return multiplicity != null && multiplicity.startsWith("0..");
    }
}
